package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class ShopBar
	{
		public var slots:int = 5;
		public var display:Sprite;
		private var m_storeSlotContainer:Sprite = new Sprite();
		private var m_storeBySlotNum:Array = new Array();
		
		public function ShopBar(width:int, height:int)
		{
			var storeConfigs:Array = Game.config.stores;
			var i:int = 0, l:int;
			
			for each(var storeConfig:Object in storeConfigs){
				createSlot(storeConfig['name']);
				m_storeBySlotNum[i++] = storeConfig;
			}
			display = Util.getRoundRectSprite(m_storeSlotContainer.width + Game.PADDING, m_storeSlotContainer.height + Game.PADDING, 0, .7, Game.PADDING);
			display.addChild(m_storeSlotContainer);
			m_storeSlotContainer.x = Game.PADDING/2;
			m_storeSlotContainer.y = Game.PADDING/2;
		}
		
		private function createSlot(storeName:String):void{
			
			var store:Store = new Store();
			var cost:TextField = new TextField();
			
			store.init(storeName);
			Util.applyStyleH2(cost, 0x33EE33);
			cost.text = '$' + store.value;
			cost.autoSize = TextFieldAutoSize.LEFT;
			
			var shopCard:Sprite = new Sprite();
			
			
			shopCard.addChild(store.display);
			store.display.x = Game.PADDING/2;
			store.display.y = Game.PADDING/2;
			shopCard.addChild(cost);
			cost.x = shopCard.width/2 - cost.width/2;
			cost.y = shopCard.y + shopCard.height + Game.PADDING/2;
			
			var shopCardBG:Sprite = Util.getRoundRectSprite(shopCard.width + Game.PADDING, shopCard.height + Game.PADDING);
			shopCardBG.addChild(shopCard);
			Util.buttonize(shopCardBG);
			shopCardBG.x = m_storeSlotContainer.numChildren * (shopCardBG.width + Game.PADDING);
			m_storeSlotContainer.addChild(shopCardBG);
			shopCardBG.addEventListener(MouseEvent.CLICK, onShopClick);
		}
		
		private function onShopClick(me:MouseEvent):void{
			var slotNum:int = (me.currentTarget as DisplayObject).parent.getChildIndex(me.currentTarget as DisplayObject);
			var storeConfig:Object = m_storeBySlotNum[slotNum];
			Game.instance.buyStoreMode(storeConfig['name']);
		}
		
	}
}